﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Agile_E
{

    public partial class getEmployee : System.Web.UI.Page
    {
        private static string empid;
        private static int db_e_id;
        private static Employees emp = new Employees();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!User.IsInRole("Manager"))
            {
                btnEdit.Visible = false;
                btnEdit.Enabled = false;
            }

            if (!IsPostBack)
            {
                empid = Request.QueryString["EmpID"].ToString();
                try
                {
                    DatabaseHandler.connect();
                    emp = DatabaseHandler.getEmployeeByID(empid);
                    db_e_id = emp.DB_E_ID;
                    txtEmpID.Text = emp.IDNumber;
                    txtlname.Text = emp.Info.LastName;
                    txtmname.Text = emp.Info.MiddleName;
                    txtfname.Text = emp.Info.FirstName;
                    txtPos.Text = emp.Position;
                    txtSuper.Text = emp.Supervisor;
                    txtWphone.Text = emp.Info.Workphone;
                    txtcphone.Text = emp.Info.Cell;
                    txthphone.Text = emp.Info.Homephone;
                    txtdealer.Text = emp.Dealership;
                    ddldealerships.SelectedIndex = emp.Dealership_ID;
                    ddldealerships.SelectedValue = emp.Dealership;
                    //ddldealerships.Visible = false;
                    ((Main)((SearchMP)this.Master).Master).clearStatus();
                }
                catch (Exception ex)
                {
                    ((Main)((SearchMP)this.Master).Master).writeStatus(ex.Message, true);
                }
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            if(User.IsInRole("Manager"))
            {
                btnEdit.Text = "Save";
                onlyRead(false);
                ddldealerships.Visible = true;
                ddldealerships.DataSourceID = "srcddlDealer";
                srcddlDealer.SelectCommand = "Select distinct(Name) from Dealerships";
                txtdealer.Visible = false;
                btnEdit.Visible = false;
                btnSave.Visible = true;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Employees updatedEmp = new Employees();
                updatedEmp.DB_E_ID = db_e_id;
                updatedEmp.Info.FirstName = txtfname.Text;
                updatedEmp.Info.MiddleName = txtmname.Text;
                updatedEmp.Info.LastName = txtlname.Text;
                updatedEmp.IDNumber = txtEmpID.Text;
                updatedEmp.Position = txtPos.Text;
                updatedEmp.Supervisor = txtSuper.Text;
                updatedEmp.Info.Workphone = txtWphone.Text;
                updatedEmp.Info.Homephone = txthphone.Text;
                updatedEmp.Info.Cell = txtcphone.Text;
                updatedEmp.Dealership = ddldealerships.SelectedValue;
                updatedEmp.Dealership_ID = ddldealerships.SelectedIndex + 1;
                updatedEmp.Specialty = txtSpecial.Text;

                DatabaseHandler.connect();
                DatabaseHandler.updateEmployee(updatedEmp);
            }

            catch (Exception ex)
            {
                ((Main)((SearchMP)this.Master).Master).writeStatus(ex.Message, true);
            }
            onlyRead(true);
            ddldealerships.Visible = false;
            txtdealer.Visible = true;
            btnEdit.Visible = true;
            btnSave.Visible = false;
            txtdealer.Text = ddldealerships.SelectedValue;
        }

        #region change readonly settings
        private void onlyRead(bool write)
        {
            txtlname.ReadOnly = write;
            txtmname.ReadOnly = write;
            txtfname.ReadOnly = write;
            txtPos.ReadOnly = write;
            txtSuper.ReadOnly = write;
            txtWphone.ReadOnly = write;
            txtcphone.ReadOnly = write;
            txthphone.ReadOnly = write;
            txtdealer.ReadOnly = write;
            txtSpecial.ReadOnly = write;
            if (!write)
            {
                txtlname.BorderColor = System.Drawing.Color.Gray;
                txtmname.BorderColor = System.Drawing.Color.Gray;
                txtfname.BorderColor = System.Drawing.Color.Gray;
                txtPos.BorderColor = System.Drawing.Color.Gray;
                txtSuper.BorderColor = System.Drawing.Color.Gray;
                txtWphone.BorderColor = System.Drawing.Color.Gray;
                txtcphone.BorderColor = System.Drawing.Color.Gray;
                txthphone.BorderColor = System.Drawing.Color.Gray;
                txtdealer.BorderColor = System.Drawing.Color.Gray;

                txtlname.BorderStyle = BorderStyle.Inset;
                txtlname.BorderWidth = Unit.Pixel(1);
                txtmname.BorderStyle = BorderStyle.Inset;
                txtmname.BorderWidth = Unit.Pixel(1);
                txtfname.BorderStyle = BorderStyle.Inset;
                txtfname.BorderWidth = Unit.Pixel(1);
                txtPos.BorderStyle = BorderStyle.Inset;
                txtPos.BorderWidth = Unit.Pixel(1);
                txtSuper.BorderStyle = BorderStyle.Inset;
                txtSuper.BorderWidth = Unit.Pixel(1);
                txtWphone.BorderStyle = BorderStyle.Inset;
                txtWphone.BorderWidth = Unit.Pixel(1);
                txtcphone.BorderStyle = BorderStyle.Inset;
                txtcphone.BorderWidth = Unit.Pixel(1);
                txthphone.BorderStyle = BorderStyle.Inset;
                txthphone.BorderWidth = Unit.Pixel(1);
                txtdealer.BorderStyle = BorderStyle.Inset;
                txtdealer.BorderWidth = Unit.Pixel(1);
            }
            else
            {
                txtlname.BorderColor = System.Drawing.Color.White;
                txtmname.BorderColor = System.Drawing.Color.White;
                txtfname.BorderColor = System.Drawing.Color.White;
                txtPos.BorderColor = System.Drawing.Color.White;
                txtSuper.BorderColor = System.Drawing.Color.White;
                txtWphone.BorderColor = System.Drawing.Color.White;
                txtcphone.BorderColor = System.Drawing.Color.White;
                txthphone.BorderColor = System.Drawing.Color.White;
                txtdealer.BorderColor = System.Drawing.Color.White;

                txtlname.BorderStyle = BorderStyle.None;
                txtmname.BorderStyle = BorderStyle.None;
                txtfname.BorderStyle = BorderStyle.None;
                txtPos.BorderStyle = BorderStyle.None;
                txtSuper.BorderStyle = BorderStyle.None;
                txtWphone.BorderStyle = BorderStyle.None;
                txtcphone.BorderStyle = BorderStyle.None;
                txthphone.BorderStyle = BorderStyle.None;
                txtdealer.BorderStyle = BorderStyle.None;
            }
        }
        #endregion

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
            {
                ((Main)((SearchMP)this.Master).Master).writeStatus("You do not have access to remove dealership", true);
            }
            else
            {
                try
                {
                    DatabaseHandler.connect();
                    emp = DatabaseHandler.getEmployeeByID(empid);
                    DatabaseHandler.removeEmployee(emp);
                    ((Main)((SearchMP)this.Master).Master).writeStatus("Employee Removed", false);
                    btnDelete.Visible = false;
                    btnEdit.Visible = false;
                }
                catch (Exception ex)
                {
                    ((Main)((SearchMP)this.Master).Master).writeStatus(ex.Message, true);
                }
            }
        }

    }
}
